package com.zongzi.stock.module;

import android.app.Activity;
import android.graphics.Color;
import android.os.SystemClock;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.zongzi.stock.R;
import com.zongzi.stock.bean.DailyDataBean;
import com.zongzi.stock.utils.MathUtils;
import com.zongzi.stock.utils.SharePreferenceUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Dell on 2017/3/28.
 */

public class StockListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Activity mActivity;
    private List<String> mStockNums;
    private DailyDataBean mDailyDataBean;

    public StockListAdapter(Activity activity, List<String> stockNums,
                            DailyDataBean dailyDataBean) {
        mActivity = activity;
        mStockNums = stockNums;
        mDailyDataBean = dailyDataBean;
        Logger.d("进入StockListAdapter");
    }

    //自定义监听事件
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public void onItemMove(int fromPosition, int toPosition) {

        String num = mStockNums.get(fromPosition);
        mStockNums.remove(fromPosition);
        mStockNums.add(toPosition, num);
        SharePreferenceUtils.putStringList(HomeActivity.STOCK_NUM, mStockNums, mActivity);
        DailyDataBean.DataBean dataBean = mDailyDataBean.data.get(fromPosition);
        mDailyDataBean.data.remove(fromPosition);
        mDailyDataBean.data.add(toPosition, dataBean);

        notifyItemMoved(fromPosition, toPosition);
    }

    public void setItemBgNor(RecyclerView.ViewHolder viewHolder) {
        //设置正常颜色
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mView = LayoutInflater.from(mActivity)
                .inflate(R.layout.adapter_stock, parent, false);
        return new MyViewHolder(mView);
    }

    public class MyThread extends Thread {
        private RecyclerView.ViewHolder viewHolder;

        public MyThread(RecyclerView.ViewHolder holder) {
            viewHolder = holder;
        }

        @Override
        public void run() {
            SystemClock.sleep(1000);
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ((MyViewHolder) viewHolder).mLlStock.setBackgroundColor(Color.WHITE);
                }
            });

        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        MyThread myThread = ((MyViewHolder) holder).myThread;

        if (position < mDailyDataBean.data.size() && mDailyDataBean.data.get(position) != null) {
            ((MyViewHolder) holder).mTvStockName.setText(mDailyDataBean.data.get(position).stockName + "");
            ((MyViewHolder) holder).mTvStockNum.setText(mDailyDataBean.data.get(position).stockCode + "");


            double price = mDailyDataBean.data.get(position).close;

            double preClose = mDailyDataBean.data.get(position).preClose;
            double changeRatio = mDailyDataBean.data.get(position).netChangeRatio;

            double changePrice = MathUtils.nRound(price - preClose, 2);
            if (changePrice > 0) {
                ((MyViewHolder) holder).mTvPrice.setTextColor(((MyViewHolder) holder).mTvPrice
                        .getResources().getColor(R.color.tv_stock_increase));
                ((MyViewHolder) holder).mTvRiseRatio.setTextColor(((MyViewHolder) holder).mTvRiseRatio
                        .getResources().getColor(R.color.tv_stock_increase));
                ((MyViewHolder) holder).mTvRisePrice.setTextColor(((MyViewHolder) holder).mTvRisePrice
                        .getResources().getColor(R.color.tv_stock_increase));
            } else if (changePrice == 0) {
                ((MyViewHolder) holder).mTvPrice.setTextColor(((MyViewHolder) holder).mTvPrice
                        .getResources().getColor(R.color.tv_stock_normal));
                ((MyViewHolder) holder).mTvRiseRatio.setTextColor(((MyViewHolder) holder).mTvRiseRatio
                        .getResources().getColor(R.color.tv_stock_normal));
                ((MyViewHolder) holder).mTvRisePrice.setTextColor(((MyViewHolder) holder).mTvRisePrice
                        .getResources().getColor(R.color.tv_stock_normal));
            } else {
                ((MyViewHolder) holder).mTvPrice.setTextColor(((MyViewHolder) holder).mTvPrice
                        .getResources().getColor(R.color.tv_stock_decrease));
                ((MyViewHolder) holder).mTvRiseRatio.setTextColor(((MyViewHolder) holder).mTvRiseRatio
                        .getResources().getColor(R.color.tv_stock_decrease));
                ((MyViewHolder) holder).mTvRisePrice.setTextColor(((MyViewHolder) holder).mTvRisePrice
                        .getResources().getColor(R.color.tv_stock_decrease));
            }

            if (mDailyDataBean.data.get(position).isRed > 0) {
                if (myThread != null) {
                    myThread.stop();
                    myThread = null;
                }
                ((MyViewHolder) holder).mLlStock.setBackgroundResource(R.drawable.bg_adapter_stock_red);
                myThread = new MyThread(holder);
                myThread.start();
            } else if (mDailyDataBean.data.get(position).isRed == 0) {
                if (myThread != null) {
                    myThread.stop();
                    myThread = null;
                }
                ((MyViewHolder) holder).mLlStock.setBackgroundColor(Color.WHITE);
            } else {
                if (myThread != null) {
                    myThread.stop();
                    myThread = null;
                }
                ((MyViewHolder) holder).mLlStock.setBackgroundResource(R.drawable.bg_adapter_stock_green);
                myThread = new MyThread(holder);
                myThread.start();
            }
            ((MyViewHolder) holder).mTvPrice.setText(MathUtils.nRound(price, 2) + "");
            ((MyViewHolder) holder).mTvRiseRatio.setText(MathUtils.nRound(changeRatio, 2) + "%");
            ((MyViewHolder) holder).mTvRisePrice.setText(changePrice + "");

            // 如果设置了回调，则设置点击事件
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(v, position);
                    }
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemLongClick(holder.itemView, pos);
                        //设置拖拽时的颜色
                    }
                    return false;
                }
            });


            holder.itemView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return false;
                }
            });
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.ll_stock)
        LinearLayout mLlStock;
        @BindView(R.id.tv_stock_name)
        TextView mTvStockName;
        @BindView(R.id.tv_stock_num)
        TextView mTvStockNum;
        @BindView(R.id.tv_price)
        TextView mTvPrice;
        @BindView(R.id.tv_rise_ratio)
        TextView mTvRiseRatio;
        @BindView(R.id.tv_rise_price)
        TextView mTvRisePrice;

        MyThread myThread;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    public int getItemCount() {
        return mStockNums.size();
    }
}
